package com.rtx.edi.platform.company.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.company.dto.PlCompanyDTO;
import com.rtx.edi.model.company.po.PlCompany;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.company.service.PlCompanyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 公司信息表 前端控制器
 * </p>
 *
 * @author cafebabe
 * @since 2023-03-10
 */
@Api(tags = {"公司管理"})
@RestController
@RequestMapping("/v1/edi/plCompany")
public class PlCompanyController {

	@Autowired
	private PlCompanyService PlCompanyService;

	/**
	 * 获取公司列表
	 * @return
	 */
	@ApiOperation(value = "公司列表", notes = "分页获取公司列表")
	@PostMapping("selectPlCompanyList")
	public EdiResponse selectPlCompanyList(@RequestBody(required = false) PlCompanyDTO dto){
		Page<PlCompany> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
		PlCompany po = new PlCompany();
		BeanUtils.copyProperties(dto, po);
		PlCompanyService.selectPlCompanyList(pageParam,po);
		return EdiResponse.success().data("page",pageParam);
	}
	
	@ApiOperation(value = "获取公司列表信息", notes = "获取公司列表")
	@PostMapping("selectPlCompany")
	public EdiResponse selectPlCompanySelect(){
		List<PlCompany> list = PlCompanyService.selectPlCompanyList();
		List<Map> item = new ArrayList<>();
		for(PlCompany company:list) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", company.getId());
			map.put("companyNo", company.getCompanyNo());
			map.put("companyName", company.getCompanyName());
			item.add(map);
		}
		return EdiResponse.success().data("item",item);
	}

	/**
	 * 根据ID获取公司
	 * @param PlCompany
	 * @return
	 */
	@ApiOperation(value = "查询公司", notes = "根据ID查询公司")
	@PostMapping("selectPlCompanyById")
	public EdiResponse selectPlCompanyById(@RequestBody PlCompanyDTO dto){
		return EdiResponse.success().data("item",PlCompanyService.getById(dto.getId()));
	}
	/**
	 * 根据id删除
	 * @param PlCompany
	 * @return
	 */
	@ApiOperation(value = "删除公司", notes = "根据id删除公司", httpMethod = "POST")
	@PostMapping("deletePlCompanyById")
	public EdiResponse deletePlCompanyById(@RequestBody PlCompanyDTO dto) {
		return PlCompanyService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
	}
	
    /**
     * 根据id列表删除
     * @param list
     * @return
     */
    @ApiOperation(value = "删除公司", notes = "根据id列表删除公司", httpMethod = "POST")
    @PostMapping("deletePlApplicationByIds")
    public EdiResponse deletePlApplicationByIds(@RequestBody List<PlCompanyDTO> list) {
    	try {
    		PlCompanyService.deletePlApplicationByIds(list);
    		return EdiResponse.success();
    	} catch (Exception e) {
    		return EdiResponse.failed().returnMgr(e.getMessage());
    	}
    }

	/**
	 * 新增公司
	 * @param PlCompany
	 * @return
	 */
	@ApiOperation(value = "新增公司")
	@PostMapping("addPlCompany")
	public EdiResponse addPlCompany(@RequestBody PlCompanyDTO dto){
		PlCompany po = new PlCompany();
		QueryWrapper<PlCompany> qw = new QueryWrapper<PlCompany>();
		qw.eq("company_name", dto.getCompanyName());
		boolean exists = PlCompanyService.getBaseMapper().exists(qw);
		if(exists) {
			return EdiResponse.failed().returnMgr("公司名称"+dto.getCompanyName()+"已存在不允许创建");
		}
		BeanUtils.copyProperties(dto, po);
		return PlCompanyService.save(po)  ? EdiResponse.success() : EdiResponse.failed();
	}

	/**
	 * 根据ID修改公司
	 * @return
	 */
	@ApiOperation(value = "修改公司", notes = "根据ID修改公司")
	@PostMapping("editPlCompanyById")
	public EdiResponse editPlCompanyById(@RequestBody PlCompanyDTO dto){
		PlCompany po = new PlCompany();
		BeanUtils.copyProperties(dto, po);
		return PlCompanyService.updateById(po) ? EdiResponse.success() : EdiResponse.failed();
	}
}
